#!/usr/bin/env python
#--*-- coding: UTF-8 --*--

import sys      # 为了参数
import urllib, urllib2  # 发送URL的包
import cookielib
import httplib, socket
import demjson  # json的解码、编码
import time, datetime;  # 时间
import MySQLdb;
import os; # 操作路径。

from configs import configs; # 引入配置项。
from CrawlerDB import CrawlerDB; # 引入数据库操作类

def downByUrllib(url, path, fileName):
    urllib.urlretrieve(url, path + fileName);

def downByUrllib2(url, path, fileName):
    heads = {
        'Referer': 'http://www.zngirls.com/g/20542/2.html',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36'
    };
    cj=cookielib.LWPCookieJar();
    opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
    urllib2.install_opener(opener);
    req=urllib2.Request(url);
    opener.addheaders = heads.items();
    operate=opener.open(req);
    data=operate.read();

    if data == None:
        print "图片下载失败";

    file = open(path + fileName, "wb");
    file.write(data);
    file.flush();
    file.close();

def downImage(url, path, fileName):
    try:
        if not os.path.exists(path):
            os.makedirs(path);

        if os.path.isfile(path):
            print("这是个错误，异常退出吧！");

        if not path.endswith("/"):
            path = path + "/";

        # downByUrllib(url, path, fileName);
        downByUrllib2(url, path, fileName);
        return True;

    except BaseException, e:
        print e;
        return False;

def downPage(db, id):
    images = db.getUndownImagesInPage(id);

    if images == None:
        return False;
    basepath = configs['image']['basepath'];

    if not basepath.endswith("/"):
        basepath = basepath + "/";

    for image in images:
        try:
            # print("\t>>> downing image: %s/%s." % (image[2], image[1]));
            sys.stdout.flush();
            if downImage(image[3], basepath + str(image[2]).zfill(5), image[1]):
                # print("\t>>> downed image: %s/%s." % (image[2], image[1]));
                # sys.stdout.flush();
                db.flagImage(image[0]);
                # print("\t>>> saved image: %s/%s." % (image[2], image[1]));
                # sys.stdout.flush();
            else:
                print("\t>>> fail to down image: %s/%s." % (image[2], image[1]));
                sys.stdout.flush();

        except BaseException, e:
            print e;

    sys.stdout.flush();
    return True;

def main(startId, endId):
    socket.setdefaulttimeout(30);
    db = CrawlerDB();
    index = startId;

    try:
        while index <= endId:
            print("Down images in page: %d" % (index));
            downPage(db, index);
            index += 1;
    finally:
        db.close();

def test():
    heads = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': '__cfduid=d138b1ce92025f32697152abcf10383931475161071',
        'Host': 't1.zngirls.com',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36'
    };
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor());
    urllib2.install_opener(opener);
    req = urllib2.Request(url);
    opener.addheaders = heads.items();
    img = opener.open(req).read();
    urllib.urlretrieve("http://t1.zngirls.com/gallery/23304/20547/014.jpg", "test-014.jpg");

def test2():
    downByUrllib2("http://t1.zngirls.com/gallery/23304/20547/014.jpg", "/home/peter/", "test-014.jpg");

startId = 9973; #9700;
limit = 21;
endId = 10216; #9972; # 9744;
if len(sys.argv) >= 2:
    arg1  = int(sys.argv[1]);

    if arg1 > startCode:
        startId = arg1;

if len(sys.argv) >= 3:
    limit = int(sys.argv[2]);

print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "\tDownlads Start");
main(startId, endId);
#test("http://img.zngirls.com/gallery/20886/15338/093.jpg");
#test2();
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "\tDownlads Over");
